package game.gol;


public class GameOfLifeRules
{
    public static final GameOfLifeRules DEFAULT = new GameOfLifeRules("23/3");
    public static final GameOfLifeRules X000001 = new GameOfLifeRules("3/3");
    public static final GameOfLifeRules X000002 = new GameOfLifeRules("13/3");
    public static final GameOfLifeRules X000003 = new GameOfLifeRules("34/3");
    public static final GameOfLifeRules X000004 = new GameOfLifeRules("35/3");
    public static final GameOfLifeRules X000005 = new GameOfLifeRules("2/3");
    public static final GameOfLifeRules X000006 = new GameOfLifeRules("24/3");
    public static final GameOfLifeRules X000007 = new GameOfLifeRules("245/3");
    public static final GameOfLifeRules X000008 = new GameOfLifeRules("125/36");
    public static final GameOfLifeRules X000009 = new GameOfLifeRules("236/3");
    public static final GameOfLifeRules X000010 = new GameOfLifeRules("135/35");
    public static final GameOfLifeRules X000011 = new GameOfLifeRules("12345/3");
    public static final GameOfLifeRules X000012 = new GameOfLifeRules("1357/1357");
    public static final GameOfLifeRules X000013 = new GameOfLifeRules("0123/01234");
    
    
    private boolean[]                   survive = new boolean[] { false, false, false, false, false, false, false, false, false };
    private boolean[]                   birth   = new boolean[] { false, false, false, false, false, false, false, false, false };

    public GameOfLifeRules(String rule)
    {
        int trenner = rule.indexOf("/");
        if (trenner <= 0)
        {
            throw new IllegalArgumentException("illegal rule: " + rule);
        }
        String ruleSurvive = rule.substring(0, trenner);
        for (int i = 0; i < ruleSurvive.length(); i++)
        {
            int index = Integer.parseInt(ruleSurvive.substring(i, i + 1));
            survive[index] = true;
        }
        String ruleBirts = rule.substring(trenner + 1);
        for (int i = 0; i < ruleBirts.length(); i++)
        {
            int index = Integer.parseInt(ruleBirts.substring(i, i + 1));
            birth[index] = true;
        }
    }
    
    private void checkNeighborCount(int neighbors)
    {
        if (neighbors < 0 || neighbors > 8)
        {
            throw new IllegalArgumentException("neighbors != 8 : " + neighbors);
        }
    }
    
    public boolean isSurviving(boolean isAlive, int neighbors)
    {
        checkNeighborCount(neighbors);
        boolean result = isAlive;
        if (isAlive)
        {
            result = survive[neighbors];
        }
        else
        {
            result = birth[neighbors];
        }
        return result;
    }
    
    public boolean isSurviving(int neighbors)
    {
        checkNeighborCount(neighbors);
        boolean result = false;
        result = survive[neighbors];
        return result;
    }
    
    public boolean isDieing(int neighbors)
    {
        checkNeighborCount(neighbors);
        boolean result = false;
        result = birth[neighbors];
        return result;
    }
}
